package com.itheima.sh.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.sh.mapper.UserMapper;
import com.itheima.sh.pojo.PageRequest;
import com.itheima.sh.pojo.PageResult;
import com.itheima.sh.pojo.User;
import com.itheima.sh.service.UserService;
import com.itheima.sh.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public List<User> findAll() {
        return this.userMapper.findAll();
    }

    @Override
    public void addUser(User user) {
        this.userMapper.addUser(user);
    }

    @Override
    public void updateUser(String username, Long id) {
        this.userMapper.updateUser(username,id);
    }

    @Override
    public void deleteUser(Long id) {
        this.userMapper.deleteUser(id);
    }

    @Override
    public PageResult findPage(PageRequest pageRequest) {
        return PageUtils.getPageResult(pageRequest, getPageInfo(pageRequest));

    }
    /**
     * 调用分页插件完成分页
     */
    private PageInfo<User> getPageInfo(PageRequest pageRequest) {
        int pageNum = pageRequest.getPageNum();
        int pageSize = pageRequest.getPageSize();
        PageHelper.startPage(pageNum, pageSize);
        List<User> sysMenus = userMapper.selectPage();
        return new PageInfo<User>(sysMenus);
    }
}
